<script setup lang="ts">
import type { AddressItem, OrderPre } from '@/types/order'
import { onMounted, ref } from 'vue'
import {
  getMedicalOrderPre,
  getAddressList,
  createMedicalOrder
} from '@/services/order'
import { useRoute } from 'vue-router'
import { Toast } from 'vant'
import OrderMedical from './components/OrderMedical.vue'

const route = useRoute()

// 1. 获取地址和预支付信息
const orderPre = ref<OrderPre>()
const addressItem = ref<AddressItem>()
onMounted(async () => {
  // 1.1 获取预支付信息
  const res1 = await getMedicalOrderPre(route.query.id as string)
  orderPre.value = res1.data
  // 1.2 获取地址信息
  const res2 = await getAddressList()
  if (res2.data.length > 0) {
    const defaultAddress = res2.data.find((item) => item.isDefault === 1)
    if (defaultAddress) {
      // 如果有默认地址, 则用默认地址
      addressItem.value = defaultAddress
    } else {
      // 如果没有默认地址, 则用数组中的第一个
      addressItem.value = res2.data[0]
    }
  }
})

// 2. 生成支付订单
const agree = ref(false)
const show = ref(false) // 控制抽屉的显示和隐藏
const orderId = ref('') // 订单id
const loading = ref(false)
const submit = async () => {
  // 2.0 校验
  if (!agree.value) return Toast('请勾选支付协议')
  if (!orderPre.value?.id) return Toast('未找到处方')
  if (!addressItem.value?.id) return Toast('请选择地址')

  // 2.1 判断是否已经有订单id
  if (!orderId.value) {
    loading.value = true
    // 没有
    try {
      const res = await createMedicalOrder({
        id: orderPre.value.id,
        addressId: addressItem.value.id,
        couponId: orderPre.value.couponId
      })
      orderId.value = res.data.id
      // 打开抽屉
      show.value = true
    } finally {
      loading.value = false
    }
  } else {
    // 有
    show.value = true
  }
}
</script>

<template>
  <div class="order-pay-page" v-if="orderPre && addressItem">
    <cp-nav-bar title="药品支付" />
    <div class="order-address">
      <p class="area">
        <van-icon name="location" />
        <span>{{
          addressItem.province + addressItem.city + addressItem.county
        }}</span>
      </p>
      <p class="detail">{{ addressItem.addressDetail }}</p>
      <p>
        {{ addressItem.receiver }}
        {{
          addressItem.mobile.replace(/^(\d{3})(?:\d{4})(\d{4})$/, '\$1****\$2')
        }}
      </p>
    </div>
    <!-- 药品列表 -->
    <order-medical :medicines="orderPre.medicines"></order-medical>
    <div class="order-detail">
      <van-cell-group>
        <van-cell
          title="药品金额"
          :value="`￥${orderPre.payment.toFixed(2)}`"
        />
        <van-cell title="运费" :value="`￥${orderPre.expressFee.toFixed(2)}`" />
        <van-cell
          title="优惠券"
          :value="`￥${orderPre.couponDeduction.toFixed(2)}`"
        />
        <van-cell
          title="实付款"
          :value="`￥${orderPre.actualPayment.toFixed(2)}`"
          class="price"
        />
      </van-cell-group>
    </div>
    <div class="order-tip">
      <p class="tip">
        由于药品的特殊性，如非错发、漏发药品的情况，药品一经发出
        不得退换，请核对药品信息无误后下单。
      </p>
      <van-checkbox v-model="agree"
        >我已同意<a href="javascript:;">支付协议</a></van-checkbox
      >
    </div>
    <van-submit-bar
      :price="orderPre.actualPayment * 100"
      button-text="立即支付"
      button-type="primary"
      text-align="left"
      @click="submit"
    ></van-submit-bar>
    <!-- 支付抽屉组件 -->
    <cp-pay-sheet
      v-model:show="show"
      :actual-payment="orderPre.actualPayment"
      :order-id="orderId"
      pay-callback="/order/pay/result"
    ></cp-pay-sheet>
  </div>
</template>

<style lang="scss" scoped>
:deep(.van-nav-bar) {
  background-color: var(--cp-primary);
  .van-nav-bar__arrow,
  .van-nav-bar__title {
    color: #fff;
  }
}
:deep(.van-cell) {
  .van-cell__title {
    font-size: 16px;
  }
  .van-cell__value {
    font-size: 16px;
  }
  &.price {
    .van-cell__value {
      font-size: 18px;
      color: var(--cp-price);
    }
  }
}
:deep(.van-submit-bar) {
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
  .van-button {
    width: 200px;
  }
}
.order-pay-page {
  padding: 46px 0 65px;
}
.order-address {
  padding: 15px 15px 0 15px;
  background-color: #fff;
  font-size: 13px;
  .area {
    color: var(--cp-tag);
    margin-bottom: 5px;
    .van-icon-location {
      color: #ff7702;
      font-size: 14px;
    }
  }
  .detail {
    font-size: 17px;
    margin-bottom: 5px;
  }
  &::after {
    content: '';
    display: block;
    height: 12px;
    background-color: var(--cp-bg);
    margin: 0 -15px;
    margin-top: 15px;
  }
}

.order-tip {
  padding: 0 15px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 10px;
  .tip {
    font-size: 12px;
    color: var(--cp-tag);
    width: 100%;
    &::before {
      content: '*';
      color: var(--cp-price);
      font-size: 14px;
    }
    margin-bottom: 30px;
  }
  .van-checkbox {
    a {
      color: var(--cp-primary);
    }
  }
}
</style>
